package com.jeecms.wicool.dao.user.impl;


import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.product.ProductDao;
import com.jeecms.wicool.dao.user.AddressDao;
import com.jeecms.wicool.dao.user.UserInfoDao;
import com.jeecms.wicool.entity.product.Product;
import com.jeecms.wicool.entity.user.Address;
import com.jeecms.wicool.entity.user.UserInfo;

@Repository
public class AddressDaoImpl extends HibernateBaseDao<Address, Integer>
		implements AddressDao {

	@Override
	protected Class<Address> getEntityClass() {
		// TODO Auto-generated method stub
		return Address.class;
	}
	@Override
	public Pagination getPage(Integer shopId,String userName,String consigneeName,
			int pageNo, int pageSize) {
			Finder f = Finder.create("select bean from Address bean where 1=1 ");
			if (!StringUtils.isBlank(userName)){
				f.append(" and bean.user.name = :userName");
				f.setParam("userName",userName);
			}if (null!=shopId){
				f.append(" and bean.shop.id = :shopId");
				f.setParam("shopId",shopId);
			}
			if(!StringUtils.isBlank(consigneeName)){
				f.append(" and bean.consigneeName like :consigneeName");
				f.setParam("consigneeName","%"+ consigneeName+"%");
			}
			f.append(" order by bean.id desc");
			return find(f, pageNo, pageSize);
	}
	@Override
	public Address findById(Integer id) {
		Address entity = get(id);
		return entity;
	}
	@Override
	public Address save(Address bean) {
		getSession().save(bean);
		return bean;
	}
	@Override
	public Address deleteById(Integer id) {
		Address entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	@Override
	public List<Address> getListAddressByUserId(Integer userId) {
		Finder f = Finder.create("select bean from Address bean where bean.user.id = '"+ userId+"'");
		return find(f);
	}
}